package kr.co.doriclassic.chapter25;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class ParseURLExample {
	public static void main(String[] args) throws IOException {
		try {
			StringBuilder sb = new StringBuilder();
			URL myURL = new URL("http://www.hkit.kr/SUB/?ctId=49&eduCode=00000027:80");
			sb.append("protocol = " + myURL.getProtocol()+"\n");
			sb.append("authority = "+myURL.getAuthority()+"\n");
			sb.append("host = "+myURL.getHost()+"\n");
			sb.append("port = "+myURL.getPort()+"\n");
			sb.append("path = "+myURL.getPath()+"\n");
			sb.append("query = "+myURL.getQuery()+"\n");
			sb.append("filename = "+myURL.getFile()+"\n");
			sb.append("ref = "+myURL.getRef());
			System.out.println(sb);
		
			//소스 읽기
			System.out.println("============================");
			BufferedReader in = new BufferedReader(new InputStreamReader(myURL.openStream()));
			String line;
			while((line=in.readLine())!=null){
				System.out.println(line);
			}
			in.close();
		} catch (MalformedURLException e) {
			System.err.println("잘못된 주소입니다.");
		}
	}
}
